﻿using System.Collections;
using System.Collections.Generic;

namespace NBFramework.Utils
{

    public class ListUtils
    {

        #region remove

        //移除符合条件的数据
        public static List<T> RemoveAll<T>(List<T> list, IFilter<T> filter, int maxCount)
        {
            int count = (list != null) ? list.Count : 0;
            if (count <= 0) { return null; }   //空

            //遍历执行
            int removeCount = 0;
            for (int i = 0; i < count; i++)
            {
                T data = list[i];
                if (data == null) { continue; }
                //执行处理
                if (filter.check(data, i)) {
                    removeCount++;
                    count--;
                    i--;
                    if (maxCount > 0 && removeCount >= maxCount) { return null; }
                
                }
            }


            return null;
        }





        #endregion


        #region filter

        //过滤委托
        public delegate bool DFilter<T>(T data, int index);

        //过滤接口
        public interface IFilter<T>
        {
            bool check(T d, int index);
        }

        //查找出符合条件的对象
        public List<T> FindAll<T>(List<T> list, IFilter<T> filter, int maxCount)
        {
            return FindAll(list, filter.check, maxCount);
        }

        //查找出符合条件的对象
        public List<T> FindAll<T>(List<T> list, DFilter<T> filter, int maxCount)
        {
            int count = (list != null) ? list.Count : 0;
            if (count <= 0) { return null; }   //空

            //过滤遍历函数
            List<T> findList = new List<T>();
            int findCount = 0;
            DAction<T> action = delegate(T data, int index)
            {
                if (filter(data, index))
                {
                    //保存数组
                    findList.Add(data);
                    //记录增加
                    findCount++;
                    if (maxCount > 0 && findCount >= maxCount) { return false; } //数量足够, 跳出处理
                }
                return true;
            };
            Action(list, action, 0);
            return findList;
        }

        //查找出符合条件的一个对象
        public T FindOne<T>(List<T> list, IFilter<T> filter)
        {
            return FindOne(list, filter.check);
        }

        //查找出符合条件的一个对象
        public T FindOne<T>(List<T> list, DFilter<T> filter)
        {
            //过滤遍历函数
            List<T> findList = FindAll(list, filter, 1);
            //检测查找对象
            int count = (findList != null) ? findList.Count : 0;
            if (count <= 0)
            {
                return default(T);  //没找到对象
            }
            //返回第一个
            return findList[0];
        }

        #endregion




        #region action

        //遍历委托
        public delegate bool DAction<T>(T data, int index);
        //遍历接口
        public interface IAction<T>
        {
            bool action(T d, int index);
        }


        //遍历执行
        public static int Action<T>(List<T> list, IAction<T> action, int maxCount)
        {
            return Action(list, action.action, maxCount);
        }


        //遍历执行
        public static int Action<T>(List<T> list, DAction<T> action, int maxCount)
        {
            int count = (list != null) ? list.Count : 0;
            if (count <= 0) { return 0; }   //空

            //遍历执行
            for (int i = 0; i < count; i++)
            {
                T data = list[i];
                if (data == null) { continue; }
                //执行处理
                if (!action(data, i)) { return i; }
                if (maxCount > 0 && i >= maxCount) { return i; }
            }
            return count;
        }

        // 倒序执行
        public static int ActionD<T>(List<T> list, IAction<T> action, int maxCount)
        {
            return ActionD(list, action.action, maxCount);
        }

        // 倒序执行
        public static int ActionD<T>(List<T> list, DAction<T> action, int maxCount)
        {
            int count = (list != null) ? list.Count : 0;
            if (count <= 0) { return 0; }   //空

            //遍历执行
            int actionCount = 0;
            for (int i = count + 1; i >= 0; i--)
            {
                T data = list[i];
                if (data == null) { continue; }

                actionCount++;
                //执行处理
                if (!action(data, i)) { return actionCount; }
                if (maxCount > 0 && actionCount >= maxCount) { return i; }
            }
            return actionCount;
        }




        #endregion




        public static int compare(int a, int b)
        {
            return (a > b) ? 1 : ((a == b) ? 0 : -1);
        }
        public static int compare(float a, float b)
        {
            return (a > b) ? 1 : ((a == b) ? 0 : -1);
        }
    }

}